package com.learn.english.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.learn.english.model.entity.CourseChapter;
import org.apache.ibatis.annotations.*;

import java.util.*;

/**
 * @author TISNIW
 * @version 1.0
 * @desc
 */
@Mapper
public interface CourseChapterMapper extends BaseMapper<CourseChapter> {
    @Select("select * from course_chapter where course_id = #{courseId} order by sort asc")
    List<CourseChapter> findByCourseId(Integer courseId);
    
    @Select("select * from course_chapter where id = #{id}")
    CourseChapter findById(Long id);
    
    @Insert("insert into course_chapter(course_id, title, `desc`, sort, is_published, created_at, updated_at) " +
            "values(#{courseId}, #{title}, #{desc}, #{sort}, #{isPublished}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertChapter(CourseChapter chapter);
    
    @Update("update course_chapter set title = #{title}, `desc` = #{desc}, sort = #{sort}, " +
            "is_published = #{isPublished}, updated_at = now() where id = #{id}")
    void updateChapter(CourseChapter chapter);
    
    @Delete("delete from course_chapter where id = #{id}")
    void deleteChapter(Long id);
    
    @Delete("delete from course_chapter where id in #{ids}")
    void deleteBatchChapters(@Param("ids") List<Long> ids);
    
    @Update("update course_chapter set is_published = #{isPublished} where id = #{id}")
    void updatePublished(Long id, Boolean isPublished);
}
